package com.behelpful.common.core.utils;

import com.stecol.common.core.exceptions.ParamException;

import java.io.Serializable;
import java.util.function.Function;

/**
 * 函数是工具类
 *
 * @author Alay
 * @date 2021-12-09 14:16
 */
public class IFunctions {

    /**
     * 添加前缀
     *
     * @param prefix
     * @param <T>
     * @return
     */
    public static <T> Function<T, String> strAddPrefix(String prefix) {
        String[] target = new String[2];
        // 加前缀,则将前缀目标放在索引 0 处
        target[0] = prefix;
        return new AddStrFunction(target);
    }

    /**
     * 添加后缀
     *
     * @param suffix
     * @param <T>
     * @return
     */
    public static <T> Function<T, String> strAddSuffix(String suffix) {
        String[] target = new String[2];
        // 加后缀,则将前缀目标放在索引 0 处
        target[1] = suffix;
        return new AddStrFunction(target);
    }

    /**
     * 去除前缀
     *
     * @param prefix
     * @param <T>
     * @return
     */
    public static <T> Function<T, String> strSubPrefix(String prefix) {
        return new SubStrFunction(prefix);
    }

    /**
     * 去除后缀
     *
     * @param prefix
     * @param <T>
     * @return
     */
    public static <T> Function<T, String> strSubSuffix(String prefix) {
        return new SubStrFunction<T>(prefix, false);
    }


    // ##################### 静态内部类 ################################


    /**
     * 字符串前缀，后缀添加
     *
     * @param <T>
     */
    private static class AddStrFunction<T> implements Function<T, String>, Serializable {
        private final String[] target;

        private AddStrFunction(String[] target) {
            this.target = target;
        }

        @Override
        public String apply(T t) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < target.length; i++) {
                String str = target[i];
                if (null == str) {
                    str = t.toString();
                }
                sb.append(str);
            }
            return sb.toString();
        }
    }


    /**
     * 字符串截取,前缀或者后缀（不支持中间字符串截取）
     *
     * @param <T>
     */
    private static class SubStrFunction<T> implements Function<T, String>, Serializable {
        private final String target;
        /**
         * 是否是前缀
         */
        private final boolean doPrefix;

        private SubStrFunction(String target, boolean doPrefix) {
            this.target = target;
            this.doPrefix = doPrefix = doPrefix;
        }
        public SubStrFunction(String target) {
            this.target = target;
            this.doPrefix = true;
        }
        @Override
        public String apply(T t) {
            String source = t.toString();
            if (target.length() >= source.length()) {
                ExceptionUtil.create(ParamException.class);
            }
            if (!source.contains(target)) {
                ExceptionUtil.create(ParamException.class);
            }
            // 去除前缀
            if (doPrefix) {
                source = source.substring(target.length());
            } else {
                // 去后缀
                source = source.substring(source.length() - target.length());
            }
            return source;
        }
    }

}